package com.taoye.thread;

import java.util.ArrayList;
import java.util.List;

public class Demo37 {
	public static void main(String[] args) throws InterruptedException {
		List<Integer> list = new ArrayList<>();
		Thread t = new Demo37ThreadA(list);
		t.setName("A");
		t.start();
		Thread t1 = new Demo37ThreadB(list);
		t1.setName("B");
		t1.start();
	}
}


class Demo37ThreadA extends Thread {
	private List<Integer> list;

	public Demo37ThreadA(List<Integer> list) {
		this.list = list;
	}

	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			list.add(i);
			System.out.println("添加了数字："+i);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

class Demo37ThreadB extends Thread {
	private volatile List<Integer> list;

	public Demo37ThreadB(List<Integer> list) {
		this.list = list;
	}

	@Override
	public void run() {
		while(true) {
			if(list.size() == 5) {
				break;
			}
		}
		System.out.println("B线程结束");
	}
}
